package redis_;

import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 汤舒青
 * @Version 1.0
 */


public class Redis01 {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        String s = jedis.set("tom", "123");
        System.out.println(s);
        String v = jedis.get("tom");
        System.out.println(v);
        jedis.close();
    }
    @Test
    public void testList(){
        //连接redis
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //操作redis
        jedis.lpush("list1","a","b","c");
        jedis.rpush("list1","d");
        List<String> list1 = jedis.lrange("list1", 0, 1);
        list1.forEach(s->{
            System.out.println(s);
        });
        System.out.println(jedis.llen("list1"));
        jedis.close();
    }
    @Test
    public void testHash(){
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //操作redis
        jedis.hset("hash1","a","1");
        HashMap<String, String> hash = new HashMap<>();
        hash.put("b","2");
        hash.put("c","3");
        jedis.hmset("hash1",hash);
        Map<String, String> hash1 = jedis.hgetAll("hash1");
        hash1.entrySet().forEach(h->{
                    System.out.println("key:"+h.getKey()+"value:"+h.getValue());
                });
        System.out.println(jedis.hlen("hash1"));
        jedis.close();
    }
}
